<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
  <div class="addBrand-container" id="member-add-app">
    <div class="container">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="优惠卷名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="优惠类型">
          <el-select v-model="form.couponType" placeholder="请选择活动区域">
            <el-option label="0直减现金券" value="0"></el-option>
            <el-option label="1满减现金券" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠金额">
          <el-input v-model="form.couponAmout"></el-input>
        </el-form-item>
        <el-form-item label="满减金额">
          <el-input v-model="form.fullAmount"></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-col :span="11">
            <el-date-picker
                    v-model="form.grantStartTime"
                    type="datetime"
                    placeholder="选择开始日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    >
            </el-date-picker>
          </el-col>
          <el-col class="line" :span="2">——————</el-col>
          <el-col :span="11">
            <el-date-picker
                    v-model="form.grantEndTime"
                    type="datetime"
                    placeholder="选择结束日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="有效时间">
          <el-col :span="11">
            <el-date-picker
                    v-model="form.effectiveStartTime"
                    type="datetime"
                    placeholder="生效日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    >
            </el-date-picker>
          </el-col>
          <el-col class="line" :span="2">——————</el-col>
          <el-col :span="11">
            <el-date-picker
                    v-model="form.effectiveEndTime"
                    type="datetime"
                    placeholder="失效日期时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="发放数量">
          <el-input v-model="form.quantity"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form', false)">提交</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/member.js"></script>
    <script src="../../js/validate.js"></script>
    <script src="../../js/index.js"></script>
    <script>
      new Vue({
        el: '#member-add-app',
        data() {
          return {
            id: '',
            actionType : '',
            form: {
              id:'',
              name:'',
              couponType:'',
              couponAmout:'',
              fullAmount:'',
              grantStartTime:'',
              grantEndTime:'',
              effectiveStartTime: '',
              effectiveEndTime: '',
              status:'',
              quantity:'',
              repeatQuantity:''

            }
          }
        },
        computed: {
          rules () {
            return {
              //姓名
              name: [{ required: true, 'validator': checkName, 'trigger': 'blur' }],
              'phone': [{ 'required': true, 'validator': checkEmail, 'trigger': 'blur' }],
              'idNumber': [{ 'required': true, 'validator': validID, 'trigger': 'blur' }]
            }
          }
        },
        created() {
          this.id = requestUrlParam('id')
          this.actionType = this.id ? 'edit' : 'add'
          if (this.id) {
            this.init()
          }
        },
        mounted() {
        },
        methods: {
          async init () {
            queryCouponById(this.id).then(res => {
              console.log(res)
              if (String(res.code) === '1') {
                console.log(res.data)
                this.form = res.data
                // this.ruleForm.password = ''
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            })
          },
          submitForm (formName, st) {
                if (this.actionType === 'add') {
                  const params = {
                    ...this.form,
                  }
                  addCoupon(params).then(res => {
                    if (res.code === 1) {
                      this.$message.success('员工添加成功！')
                      if (!st) {
                        this.goBack()
                      } else {
                        this.ruleForm = {
                          username: '',
                          'name': '',
                          'phone': '',
                          // 'password': '',
                          // 'rePassword': '',/
                          'sex': '男',
                          'idNumber': ''
                        }
                      }
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                } else {
                  const params = {
                    ...this.form,
                  }
                  editCoupon(params).then(res => {
                    if (res.code === 1) {
                      this.$message.success('员工信息修改成功！')
                      this.goBack()
                    } else {
                      this.$message.error(res.msg || '操作失败')
                    }
                  }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                  })
                }


          },
          goBack(){
            window.parent.menuHandle({
              id: '2',
              url: '/backend/page/coupon/list.html',
              name: '优惠券管理'
            },false)
          }
        }
      })
    </script>
</body>
</html>